//
//  SDCityViewController.m
//  SuperDoctor
//
//  Created by 李忠 on 7/6/16.
//  Copyright © 2016 SD. All rights reserved.
//

#import "SDCityViewController.h"
#import "UIBarButtonItem+SDExtension.h"
#import "NSUserDefaults+SDExtension.h"

@interface SDCityViewController ()

@end

static NSString *const cityId = @"cell";

@implementation SDCityViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupUI];
    [self setUpLeftButtonItem];
}

- (void)setupUI {
    
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:cityId];
    
}
- (void)setUpLeftButtonItem{
    
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem initWithImageStr:@"JianTou" addTarget:self withAction:@selector(goBack)];
    
}

- (void)goBack{
    
    [self.navigationController popViewControllerAnimated:YES];
    
}
#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.cities.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cityId forIndexPath:indexPath];
    
    NSString *cityStr = self.cities[indexPath.row];
    cell.textLabel.text = cityStr;
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSString *cityString = self.cities[indexPath.row];
    
    //点击了cell 就存储城市
    [[NSUserDefaults standardUserDefaults] saveString:cityString withKey:kCurrentCity];
    UIViewController *vc = self.navigationController.childViewControllers.firstObject;
    
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
    SEL sel = NSSelectorFromString(@"loadCurDayWeather:");
    if([vc respondsToSelector:sel]){
        [vc performSelector:sel withObject:cityString];
    }else{
        NSLog(@"loadCurDayWeather: 没有响应");
    }
#pragma clang diagnostic pop
    [self.navigationController popToRootViewControllerAnimated:YES];
}


@end
